<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>采购入库单</title>
    <th:block th:include="common.html :: source"></th:block>
</head>
<body>
<script type="text/javascript">
    $(function(){
        var pager = $(".easyui-datagrid").datagrid('getPager');
        $(pager).pagination({
            total:parseInt($("#total").val()),
            pageSize:parseInt($("#size").val()),
            pageNumber:parseInt($("#current").val()),
            afterPageText: '页，共{pages}页',
            pageList: [1, 5, 10, 20, 30, 50],
            //刷新方法
            onBeforeRefresh:function(current,size){
                let supplierId = $("#supplierId").val();
                let invalid = $("#invalid").val();
                let status = $("#status").val();
                let employeeName = $("#employeeName").val();
                let orderDate1 = $("#orderDate1").val();
                let orderDate2 = $("#orderDate2").val();
                location.href="/orders/list?current=" + current + "&size=" + size + "&supplierId=" + supplierId + "&invalid=" + invalid + "&status=" + status + "&employeeName=" + employeeName + "&orderDate1=" + orderDate1 + "&orderDate2=" + orderDate2;
            },
            onSelectPage:function(current,size){
                let supplierId = $("#supplierId").val();
                let invalid = $("#invalid").val();
                let status = $("#status").val();
                let employeeName = $("#employeeName").val();
                let orderDate1 = $("#orderDate1").val();
                let orderDate2 = $("#orderDate2").val();
                location.href="/orders/list?current=" + current + "&size=" + size + "&supplierId=" + supplierId + "&invalid=" + invalid + "&status=" + status + "&employeeName=" + employeeName + "&orderDate1=" + orderDate1 + "&orderDate2=" + orderDate2;
            }
        });
    });

    function add(){
        var url="/orders/init";
        var title="采购入库新增";
        addTab1(title, url);
    }

    function del(){
        var rows = $("#dgitem").datagrid("getSelections");
        if(rows.length == 0){
            $.messager.alert("操作提示", "请选择要删除的单据！","error");
            return false;
        }
        var orderNoArr = "";
        for(let i=0; i<rows.length; i++) {
            let row = rows[i];
            if (row.status == '已审核') {
                $.messager.alert("操作提示", "包含已审核的订单，无法删除！", "error");
                return false;
            }
            if (row.invalid == '作废') {
                $.messager.alert("操作提示", "包含作废的订单，无法删除！", "error");
                return false;
            }
            if((i+1) != rows.length){
                orderNoArr += row.orderNo+",";
            }else{
                orderNoArr += row.orderNo;
            }
        }

        $.messager.confirm('提示','是否删除？',function(res){
            if(res){
                $.post(
                    "/orders/batchDelete?orderNoArr=" + orderNoArr,
                    function (data) {
                        if(data == "success"){
                            $.messager.alert("操作提示", "删除成功！","info",function () {
                                location.href = '/orders/list';
                            });
                        }else{
                            $.messager.alert("操作提示", "删除失败！","error");
                        }
                    }
                );
            }
        })
    }

    function verify(){
        var rows = $("#dgitem").datagrid("getSelections");
        if(rows.length <= 0){
            $.messager.alert("操作提示", "请选择要审核的单据！","error");
            return false;
        }
        var orderNoArr = "";
        for(let i=0; i<rows.length; i++) {
            let row = rows[i];
            if (row.status == '已审核') {
                $.messager.alert("操作提示", "包含已审核的订单，无法重复审核！", "error");
                return false;
            }
            if (row.invalid == '作废') {
                $.messager.alert("操作提示", "包含作废的订单，无法审核！", "error");
                return false;
            }
            if((i+1) != rows.length){
                orderNoArr += row.orderNo+",";
            }else{
                orderNoArr += row.orderNo;
            }
        }

        $.messager.confirm('提示','确定审核？',function(res){
            if(res){
                $.post(
                    "/orders/batchVerify?orderNoArr=" + orderNoArr,
                    function (data) {
                        if(data == "success"){
                            $.messager.alert("操作提示", "审核成功！","info",function () {
                                location.href = '/orders/list';
                            });
                        }else{
                            $.messager.alert("操作提示", "审核失败！","error");
                        }
                    }
                );
            }
        })
    }

    function invalid(){
        var rows = $("#dgitem").datagrid("getSelections");
        if(rows.length <= 0){
            $.messager.alert("操作提示", "请选择要作废的单据！","error");
            return false;
        }
        var orderNoArr = "";
        for(let i=0; i<rows.length; i++) {
            let row = rows[i];
            if (row.status == '已审核') {
                $.messager.alert("操作提示", "包含已审核的订单，无法作废！", "error");
                return false;
            }
            if (row.invalid == '作废') {
                $.messager.alert("操作提示", "包含作废的订单，无法重复作废！", "error");
                return false;
            }
            if((i+1) != rows.length){
                orderNoArr += row.orderNo+",";
            }else{
                orderNoArr += row.orderNo;
            }
        }

        $.messager.confirm('提示','是否作废？',function(res){
            if(res){
                $.post(
                    "/orders/batchInvalid?orderNoArr=" + orderNoArr,
                    function (data) {
                        if(data == "success"){
                            $.messager.alert("操作提示", "作废成功！","info",function () {
                                location.href = '/orders/list';
                            });
                        }else{
                            $.messager.alert("操作提示", "作废失败！","error");
                        }
                    }
                );
            }
        })
    }

    function edit(){
        var rows = $("#dgitem").datagrid("getSelections");
        if(rows.length <= 0){
            $.messager.alert("操作提示", "请选择要查看的单据！","error");
            return false;
        }

        if(rows.length > 1){
            $.messager.alert("操作提示", "只能选择一张单据！","error");
            return false;
        }
        var url = "/orders/edit?orderNo="+rows[0].orderNo;
        var title="物料入库维护"+rows[0].orderNo;
        addTab1(title, url);
    }

    function ordersSearch() {
        document.forms[0].submit();
    }
</script>
<form action="/orders/list" method="post">
    <table width="100%" border="0">
        <tr>
            <td>
                <a class="easyui-linkbutton" plain="true" icon="icon-add" href="javascript:void(0);" onclick="add()">添加</a>
                <a class="easyui-linkbutton" plain="true" icon="icon-cut" href="javascript:void(0);" onclick="del()">删除</a>
                <a class="easyui-linkbutton" plain="true" icon="icon-edit" href="javascript:void(0);" onclick="verify()">审核</a>
                <a class="easyui-linkbutton" plain="true" icon="icon-no" href="javascript:void(0);" onclick="invalid()">作废</a>
                <a class="easyui-linkbutton" plain="true" icon="icon-tip" href="javascript:void(0);" onclick="edit()">单据详情</a>
            </td>
        </tr>
        <tr>
            <td align="right">
                供应商<select name="supplierId" th:value="${form.supplierId}" id="supplierId" style="width:160px">
                <option value="">不选择</option>
                <option th:each="supplier:${supplierList}"
                        th:value="${supplier.supplierId}"
                        th:text="'('+${supplier.supplierCode}+')'+${supplier.supplierName}"
                        th:selected="${supplier.supplierId == form.supplierId}"
                ></option>
            </select>
                单据状态
                <select name="invalid" id="invalid" th:value="${form.invalid}"  style="width:100px">
                    <option value="" th:selected="${form.invalid == ''}">不选择</option>
                    <option value="1" th:selected="${form.invalid == 1}">有效</option>
                    <option value="0" th:selected="${form.invalid == 0}">作废</option>
                </select>
                审核状态
                <select name="status" id="status" th:value="${form.status}" style="width:100px">
                    <option value="" th:selected="${form.status == ''}">不选择</option>
                    <option value="1" th:selected="${form.status == 1}">已审核</option>
                    <option value="0" th:selected="${form.status == 0}">未审核</option>
                </select>
                开单人<input type="text" name="employeeName" th:value="${form.employeeName}" id="employeeName" style="width:160px">
                时间<input type="text" th:value="${form.orderDate1}" class="easyui-datebox" name="orderDate1" id="orderDate1" style="width:160px">
                -<input type="text" th:value="${form.orderDate2}" class="easyui-datebox" name="orderDate2" id="orderDate2"  style="width:160px">
                <a href="javascript:void(0);" class="easyui-linkbutton" plain="true" onclick="ordersSearch()" iconCls="icon-search" >搜索</a>
            </td>
        </tr>
    </table>
</form>
<input type="hidden" id="total" th:value="${page.total}"/>
<input type="hidden" id="size" th:value="${page.size}"/>
<input type="hidden" id="current" th:value="${page.current}"/>
<table class="easyui-datagrid" pagination="true" id="dgitem" style="width:auto;height:420px"  singleSelect="false">
    <thead>
    <tr>
        <th field="orderNo" checkbox="true" >选择</th>
        <th field="orderType" align="center" width="70">单据类型</th>
        <th field="orderNo1" align="center" width="140">单据编号</th>
        <th field="supplierName" align="center" width="150">供应商</th>
        <th field="employeeName" align="center" width="80">开单人</th>
        <th field="oprDate" align="center" width="150">开单时间</th>
        <th field="invalid" align="center" width="60">单据状态</th>
        <th field="status" align="center" width="60">审核状态</th>
        <th field="materialCode" align="center" width="60">物料编码</th>
        <th field="materialName" align="center" width="80">物料名称</th>
        <th field="batchNo" align="center" width="140">批号</th>
        <th field="style" align="center" width="60">规格</th>
        <th field="storageName" align="center" width="60">仓库</th>
        <th field="materialUnit" align="center" width="60">单位</th>
        <th field="orderCnt" align="center" width="60">数量</th>
        <th field="orderFlag" align="center" width="60">冲红</th>
        <th field="aduitPerson" align="center" width="80">审核人</th>
        <th field="aduitDate" align="center" width="140">审核时间</th>
    </tr>
    </thead>
    <tbody>
        <tr th:each="orders:${page.data}">
            <td th:text="${orders.orderNo}"></td>
            <td th:if="${orders.orderType == 1}">采购入库单</td>
            <td th:if="${orders.orderType == 2}">采购退货单</td>
            <td th:if="${orders.orderType == 3}">销售出库单</td>
            <td th:if="${orders.orderType == 4}">销售退货单</td>
            <td th:text="${orders.orderNo}"></td>
            <td th:text="${orders.supplierName}"></td>
            <td th:text="${orders.employeeName}"></td>
            <td th:text="${orders.orderDate}"></td>
            <td th:if="${orders.invalid == 1}">有效</td>
            <td th:if="${orders.invalid == 0}">作废</td>
            <td th:if="${orders.status == 1}">已审核</td>
            <td th:if="${orders.status == 0}">未审核</td>
            <td th:text="${orders.materialCode}"></td>
            <td th:text="${orders.materialName}"></td>
            <td th:text="${orders.batchNo}"></td>
            <td th:text="${orders.style}"></td>
            <td th:text="${orders.storageName}"></td>
            <td th:text="${orders.unitName}"></td>
            <td th:text="${orders.orderCount}"></td>
            <td th:text="${orders.orderFlag}"></td>
            <td th:text="${orders.verifyPerson}"></td>
            <td th:text="${orders.verifyDate}"></td>
        </tr>
    </tbody>
</table>
</body>
</html>